#!/bin/bash

# Property Cleaning System - Docker MySQL Initialization Script
# This script starts MySQL and initializes the database schema

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

# Configuration
CONTAINER_NAME="property-mysql"
ROOT_PASSWORD="root123"
DB_NAME="property_cleaning"
DB_USER="property_user"
DB_PASSWORD="property_pass"
PORT="3306"
SCHEMA_FILE="database/schema.sql"

echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}  Property Cleaning System - MySQL Setup${NC}"
echo -e "${BLUE}========================================${NC}"
echo

# Check if Docker is running
if ! command -v docker &> /dev/null; then
    echo -e "${RED}Error: Docker is not installed${NC}"
    exit 1
fi

if ! docker info &> /dev/null; then
    echo -e "${RED}Error: Docker is not running${NC}"
    echo -e "${YELLOW}Please start Docker service first${NC}"
    exit 1
fi

# Check if schema file exists
if [ ! -f "$SCHEMA_FILE" ]; then
    echo -e "${RED}Error: Schema file not found: $SCHEMA_FILE${NC}"
    echo -e "${YELLOW}Please ensure database/schema.sql exists${NC}"
    exit 1
fi

echo -e "${GREEN}Starting MySQL container...${NC}"

# Stop and remove existing container
docker stop "$CONTAINER_NAME" 2>/dev/null || true
docker rm "$CONTAINER_NAME" 2>/dev/null || true

# Start MySQL container with schema volume
docker run -d \
    --name "$CONTAINER_NAME" \
    -p "$PORT:3306" \
    -e MYSQL_ROOT_PASSWORD="$ROOT_PASSWORD" \
    -e MYSQL_DATABASE="$DB_NAME" \
    -e MYSQL_USER="$DB_USER" \
    -e MYSQL_PASSWORD="$DB_PASSWORD" \
    -v "$(pwd)/database:/docker-entrypoint-initdb.d" \
    mysql:8.0 \
    --default-authentication-plugin=mysql_native_password

echo -e "${YELLOW}Waiting for MySQL to initialize...${NC}"

# Wait for MySQL to be ready
MAX_RETRIES=30
RETRY_COUNT=0

while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
    if docker exec "$CONTAINER_NAME" mysqladmin ping -h localhost -u root -p"$ROOT_PASSWORD" --silent; then
        echo -e "${GREEN}MySQL is ready!${NC}"
        break
    fi
    
    RETRY_COUNT=$((RETRY_COUNT + 1))
    echo -n "."
    sleep 2
done

if [ $RETRY_COUNT -eq $MAX_RETRIES ]; then
    echo -e "${RED}MySQL failed to start properly${NC}"
    echo -e "${YELLOW}Check logs with: docker logs $CONTAINER_NAME${NC}"
    exit 1
fi

echo -e "${GREEN}Importing database schema...${NC}"

# Import schema
docker exec -i "$CONTAINER_NAME" mysql -uroot -p"$ROOT_PASSWORD" "$DB_NAME" < "$SCHEMA_FILE"

echo -e "${GREEN}Verifying database setup...${NC}"

# Verify tables were created
TABLE_COUNT=$(docker exec "$CONTAINER_NAME" mysql -uroot -p"$ROOT_PASSWORD" -e "SELECT COUNT(*) as count FROM information_schema.tables WHERE table_schema='$DB_NAME';" -s -N)

echo -e "${GREEN}Successfully created $TABLE_COUNT tables${NC}"

echo
echo -e "${BLUE}========================================${NC}"
echo -e "${GREEN}MySQL setup completed successfully!${NC}"
echo -e "${BLUE}========================================${NC}"
echo
echo -e "${YELLOW}Connection Information:${NC}"
echo "  Container: $CONTAINER_NAME"
echo "  Port: $PORT"
echo "  Database: $DB_NAME"
echo "  User: $DB_USER"
echo "  Password: $DB_PASSWORD"
echo
echo -e "${YELLOW}Connection commands:${NC}"
echo "  From host: mysql -h127.0.0.1 -P$PORT -u$DB_USER -p$DB_PASSWORD $DB_NAME"
echo "  From container: docker exec -it $CONTAINER_NAME mysql -u$DB_USER -p$DB_PASSWORD $DB_NAME"
echo
echo -e "${YELLOW}Management commands:${NC}"
echo "  Check status: docker ps | grep $CONTAINER_NAME"
echo "  View logs: docker logs $CONTAINER_NAME"
echo "  Stop: docker stop $CONTAINER_NAME"
echo "  Start: docker start $CONTAINER_NAME"
echo
echo -e "${BLUE}========================================${NC}"